using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.ContactsDomain;
using ShivamEMR.Model.DataInterfaces.ContactsDataInterf;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model;
//using ShivamEMR.Web.Areas.Contacts.Models;

namespace ShivamEMR.Web.Areas.Contacts.Controllers
{   
    public class AddressController : Controller
    {
		private readonly IAddressTypeRepository addresstypeRepository;
		private readonly ICityRepository cityRepository;
		private readonly ITerritoryRepository territoryRepository;
		private readonly ICountryRepository countryRepository;
		private readonly ICompanyRepository companyRepository;
		private readonly IBranchRepository branchRepository;
		private readonly IAddressRepository addressRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public AddressController() 
        {
            addresstypeRepository = Registry.RepositoryFactory.GetAddressTypeRepository();
            cityRepository = Registry.RepositoryFactory.GetCityRepository();
            territoryRepository = Registry.RepositoryFactory.GetTerritoryRepository();
            countryRepository = Registry.RepositoryFactory.GetCountryRepository();
            companyRepository = Registry.RepositoryFactory.GetCompanyRepository();
            branchRepository = Registry.RepositoryFactory.GetBranchRepository();
            addressRepository = Registry.RepositoryFactory.GetAddressRepository();
        }

        public AddressController(IAddressTypeRepository addresstypeRepository, ICityRepository cityRepository, ITerritoryRepository territoryRepository, ICountryRepository countryRepository, ICompanyRepository companyRepository, IBranchRepository branchRepository, IAddressRepository addressRepository)
        {
			this.addresstypeRepository = addresstypeRepository;
			this.cityRepository = cityRepository;
			this.territoryRepository = territoryRepository;
			this.countryRepository = countryRepository;
			this.companyRepository = companyRepository;
			this.branchRepository = branchRepository;
			this.addressRepository = addressRepository;
        }

        //
        // GET: /Address/

        public ViewResult Index()
        {
          //  return View(addressRepository.AllIncluding(address => address.AddressType, address => address.City, address => address.Territory, address => address.Country, address => address.Company, address => address.Branch, address => address.Persons));
            return View(addressRepository.All);
        }

        //
        // GET: /Address/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(addressRepository.Find(id));
        }

        //
        // GET: /Address/Create

        public ActionResult Create()
        {
			ViewBag.PossibleAddressTypes = addresstypeRepository.All;
			ViewBag.PossibleCities = cityRepository.All;
			ViewBag.PossibleTerritories = territoryRepository.All;
			ViewBag.PossibleCountries = countryRepository.All;
			ViewBag.PossibleCompanies = companyRepository.All;
			ViewBag.PossibleBranches = branchRepository.All;
            return View();
        } 

        //
        // POST: /Address/Create

        [HttpPost]
        public ActionResult Create(Address address)
        {
            if (ModelState.IsValid) {
                addressRepository.InsertOrUpdate(address);
                addressRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleAddressTypes = addresstypeRepository.All;
				ViewBag.PossibleCities = cityRepository.All;
				ViewBag.PossibleTerritories = territoryRepository.All;
				ViewBag.PossibleCountries = countryRepository.All;
				ViewBag.PossibleCompanies = companyRepository.All;
				ViewBag.PossibleBranches = branchRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Address/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleAddressTypes = addresstypeRepository.All;
			ViewBag.PossibleCities = cityRepository.All;
			ViewBag.PossibleTerritories = territoryRepository.All;
			ViewBag.PossibleCountries = countryRepository.All;
			ViewBag.PossibleCompanies = companyRepository.All;
			ViewBag.PossibleBranches = branchRepository.All;
             return View(addressRepository.Find(id));
        }

        //
        // POST: /Address/Edit/5

        [HttpPost]
        public ActionResult Edit(Address address)
        {
            if (ModelState.IsValid) {
                addressRepository.InsertOrUpdate(address);
                addressRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleAddressTypes = addresstypeRepository.All;
				ViewBag.PossibleCities = cityRepository.All;
				ViewBag.PossibleTerritories = territoryRepository.All;
				ViewBag.PossibleCountries = countryRepository.All;
				ViewBag.PossibleCompanies = companyRepository.All;
				ViewBag.PossibleBranches = branchRepository.All;
				return View();
			}
        }

        //
        // GET: /Address/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(addressRepository.Find(id));
        }

        //
        // POST: /Address/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var ad = addressRepository.Find(id);
            addressRepository.Delete(ad);
            addressRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

